package lc20240526;




public class JudgeIsCycle {
    public static void main(String[] args) {
        ListNode head=new ListNode(1);
        head.next=new ListNode(2);
        head.next.next=new ListNode(3);
        head.next.next.next=head;
        System.out.println(hasCycle(head));
    }

    public static boolean hasCycle(ListNode head) {
        if (head==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head;
        while (slow!=null){
            if(fast.next==null||fast.next.next==null){
                return false;
            }
            slow=slow.next;
            fast=fast.next.next;
            if (slow==fast){
                return true;
            }
        }
        return false;
    }
}
